package com.zmzncs.lmtc.module.order.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmzncs.lmtc.common.pojo.LoginUser;
import com.zmzncs.lmtc.common.pojo.PageInfo;
import com.zmzncs.lmtc.common.util.DateTimeUtil;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.JwtUtil;
import com.zmzncs.lmtc.module.customer.dao.CardDao;
import com.zmzncs.lmtc.module.customer.dao.MonthRecordDao;
import com.zmzncs.lmtc.module.order.dao.OrderInfoDao;
import com.zmzncs.lmtc.module.order.dao.ParkReserveRecordDao;
import com.zmzncs.lmtc.module.order.pojo.order_Info.*;
import com.zmzncs.lmtc.module.order.pojo.orderreserve.ParkReserveRecordDTO;
import com.zmzncs.lmtc.module.order.service.OrderInfoService;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDao;
import com.zmzncs.lmtc.module.park.dao.ParkManagerDao;
import com.zmzncs.lmtc.module.park.pojo.park_info.ParkInfo;
import com.zmzncs.lmtc.module.park.pojo.park_info.ParkInfoDTO;
import com.zmzncs.lmtc.module.park.pojo.park_manager.ParkManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 高博文
 * @since 2020-09-01
 */
@Slf4j
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoDao, OrderInfo> implements OrderInfoService {


    @Autowired
    OrderInfoDao orderInfoDao;
    @Autowired
    ParkInfoDao parkInfoDao;
    @Autowired
    CardDao cardDao;
    @Autowired
    ParkManagerDao parkManagerDao;
    @Autowired
    ParkReserveRecordDao parkReserveRecordDao;
    @Autowired
    MonthRecordDao monthRecordDao;

    @Autowired
    HttpServletRequest request;


    @Override
    public OrderInfoManageVO getParkingOwnerData(Long parkId) {

        //获取登录人信息
        LoginUser loginUser = JwtUtil.getLoginUser(request);
        Long parkManageId = loginUser.getCustomerId();

        OrderInfoManageVO manageVO = new OrderInfoManageVO();
        manageVO.setIsOwner(0);

        //查询车场主/管理员信息
        ParkManager parkManager = parkManagerDao.selectById(parkManageId);
        if (parkManager != null) {
            manageVO.setAvatar(parkManager.getAvatar());
            manageVO.setNickname(parkManager.getNickname());
            manageVO.setMobilePhone(parkManager.getMobilePhone());
            manageVO.setIsOwner(parkManager.getIsOwner());
            manageVO.setRealName(parkManager.getRealName());
        }

        //根据车场主查询停车场信息
        List<ParkInfoDTO> parkInfoDTOList = parkInfoDao.getParkInfoByManageId(parkManageId);
        manageVO.setParkList(parkInfoDTOList);
        if (parkInfoDTOList == null || parkInfoDTOList.isEmpty()) {
            manageVO.setTodayRevenue(new BigDecimal(0));
            manageVO.setTodayParking(0L);
            manageVO.setTotalRevenue(new BigDecimal(0));
            manageVO.setTotalParking(0L);
            manageVO.setTotalMonthCar(0L);
            return manageVO;
        }

        if (parkId == null) {
            parkId = parkInfoDTOList.get(0).getId();
            manageVO.setParkId(parkId);
            manageVO.setParkName(parkInfoDTOList.get(0).getParkName());
            manageVO.setParkSource(parkInfoDTOList.get(0).getParkSource());
        } else {
            ParkInfo parkInfo = parkInfoDao.selectById(parkId);
            manageVO.setParkId(parkInfo.getId());
            manageVO.setParkName(parkInfo.getParkName());
            manageVO.setParkSource(parkInfo.getParkSource());
        }

        //停车场今日收入
        String todayRevenue = orderInfoDao.getPaidAmountByDate(parkId, 2);
        manageVO.setTodayRevenue(new BigDecimal(todayRevenue));
        //今日泊车
        Long todayParking = orderInfoDao.getCarCountByDate(parkId, 1);
        manageVO.setTodayParking(todayParking);

        //停车场累计收入
        String totalRevenue = orderInfoDao.getPaidAmountByDate(parkId, null);
        manageVO.setTotalRevenue(new BigDecimal(totalRevenue));
        //累计泊车
        Long totalParking = orderInfoDao.getCarCountByDate(parkId, null);
        manageVO.setTotalParking(totalParking);

        //停车场月租车数量
        Long count = cardDao.getTotalMonthCarByParkId(parkId);
        Long aLong = cardDao.getTotalMonthCardPark(parkId);
        count = count + aLong;
        manageVO.setTotalMonthCar(count);

        //待办事项
        IPage<ParkReserveRecordDTO> iPage = parkReserveRecordDao.getAllReserveRecord(new Page(0, 10), parkId, 2);
        Long todoCount = iPage == null ? 0L : iPage.getTotal();
        manageVO.setTodoCount(todoCount);

        //预约申请
        IPage<ParkReserveRecordDTO> dtoiPage = parkReserveRecordDao.getSubmitReserveRecord(new Page(0, 10), parkId);
        Long reserveCount = dtoiPage == null ? 0L : dtoiPage.getTotal();
        manageVO.setReserveCount(reserveCount);

        return manageVO;
    }

    @Override
    public ParkOrderInfoVO getParkOrderInfo(Long parkId, Date selectDate,Integer currentPage,Integer pageSize) {

//        Date firstDay = DateTimeUtil.currentFirstDay(selectDate);
//        Date lastDay = DateTimeUtil.currentLastDay(selectDate);

        ParkOrderInfoVO orderInfo = new ParkOrderInfoVO();
        //本月收入
        String amount = getPaidAmountByDate(parkId, 4);
        orderInfo.setMonthRevenue(new BigDecimal(amount));
        //本月泊车
        Long count = orderInfoDao.getCarCountByDate(parkId, 2);
        orderInfo.setMonthParking(count);


        //场内车数量
        Long carCount = orderInfoDao.getCarCount(parkId);
        carCount = carCount == null ? 0L : carCount;
        orderInfo.setCarCount(carCount);

        //查询当天停车场停车信息
        IPage<OrderInfoDTO> orderInfoDTOIPage = orderInfoDao.getParkOrderInfo(new Page(currentPage, pageSize), parkId, selectDate);
        if (orderInfoDTOIPage != null){
            List<OrderInfoDTO> records = orderInfoDTOIPage.getRecords();
            if (records != null && !records.isEmpty()){
                for (OrderInfoDTO infoDTO : records) {
                    Integer cardType = infoDTO.getPayType();
                    if (cardType != null && cardType == 1){
                        infoDTO.setCarType("月租车");
                    } else if(cardType != null && cardType == 2){
                        infoDTO.setCarType("时长");
                    } else if(cardType != null && cardType == 2) {
                        infoDTO.setCarType("余额");
                    } else {
                        infoDTO.setCarType("临时");
                    }

                    BigDecimal paidAmount = infoDTO.getPaidAmount();
                    if (paidAmount == null){
                        infoDTO.setPaidAmount(new BigDecimal(0));
                    }

                    Long parkDuration = infoDTO.getParkDuration();
                    if (parkDuration == null){
                        parkDuration = 0L;
                    }
                    String parkingTime = DateTimeUtil.toHourByMinute(parkDuration);
                    infoDTO.setParkingTime(parkingTime);
                }
            }
        }
        PageInfo<ParkOrderInfo> pageInfo = JsonUtil.pageTransform(orderInfoDTOIPage, ParkOrderInfo.class);
        orderInfo.setPageInfo(pageInfo);

        return orderInfo;
    }


    /**
     * 昨天、今天、本周、本月的缴费
     */
    @Override
    public String getPaidAmountByDate(Long parkId, Integer dateType) {
        String paidAmountByDate = orderInfoDao.getPaidAmountByDate(parkId, dateType);

        return paidAmountByDate;
    }

    /**
     * 每月收益
     */
    @Override
    public Map getMonthIncome(Long parkId) {
        Map<String, String> map = new HashMap<>();
        int num = 1;
        String parkIncome = "0";
        String reserveIncome = "0";
        String monthRentIncome = "0";

        //  1
        num = 1;
        parkIncome = orderInfoDao.getMonthIncome(parkId, num);
        reserveIncome = parkReserveRecordDao.getMonthIncome(parkId, num);
        monthRentIncome = monthRecordDao.getMonthIncome(parkId, num);
        map.put("january", new BigDecimal(parkIncome).add(new BigDecimal(reserveIncome)).add(new BigDecimal(monthRentIncome)).toString() );
        //  2
        num = 2;
        parkIncome = orderInfoDao.getMonthIncome(parkId, num);
        reserveIncome = parkReserveRecordDao.getMonthIncome(parkId, num);
        monthRentIncome = monthRecordDao.getMonthIncome(parkId, num);
        map.put("february", new BigDecimal(parkIncome).add(new BigDecimal(reserveIncome)).add(new BigDecimal(monthRentIncome)).toString() );
        //  3
        num = 3;
        parkIncome = orderInfoDao.getMonthIncome(parkId, num);
        reserveIncome = parkReserveRecordDao.getMonthIncome(parkId, num);
        monthRentIncome = monthRecordDao.getMonthIncome(parkId, num);
        map.put("march", new BigDecimal(parkIncome).add(new BigDecimal(reserveIncome)).add(new BigDecimal(monthRentIncome)).toString() );
        //  4
        num = 4;
        parkIncome = orderInfoDao.getMonthIncome(parkId, num);
        reserveIncome = parkReserveRecordDao.getMonthIncome(parkId, num);
        monthRentIncome = monthRecordDao.getMonthIncome(parkId, num);
        map.put("april", new BigDecimal(parkIncome).add(new BigDecimal(reserveIncome)).add(new BigDecimal(monthRentIncome)).toString() );
        //  5
        num = 5;
        parkIncome = orderInfoDao.getMonthIncome(parkId, num);
        reserveIncome = parkReserveRecordDao.getMonthIncome(parkId, num);
        monthRentIncome = monthRecordDao.getMonthIncome(parkId, num);
        map.put("may", new BigDecimal(parkIncome).add(new BigDecimal(reserveIncome)).add(new BigDecimal(monthRentIncome)).toString() );
        //   6
        num = 6;
        parkIncome = orderInfoDao.getMonthIncome(parkId, num);
        reserveIncome = parkReserveRecordDao.getMonthIncome(parkId, num);
        monthRentIncome = monthRecordDao.getMonthIncome(parkId, num);
        map.put("june", new BigDecimal(parkIncome).add(new BigDecimal(reserveIncome)).add(new BigDecimal(monthRentIncome)).toString() );
        //  7
        num = 7;
        parkIncome = orderInfoDao.getMonthIncome(parkId, num);
        reserveIncome = parkReserveRecordDao.getMonthIncome(parkId, num);
        monthRentIncome = monthRecordDao.getMonthIncome(parkId, num);
        map.put("july", new BigDecimal(parkIncome).add(new BigDecimal(reserveIncome)).add(new BigDecimal(monthRentIncome)).toString() );
        //  8
        num = 8;
        parkIncome = orderInfoDao.getMonthIncome(parkId, num);
        reserveIncome = parkReserveRecordDao.getMonthIncome(parkId, num);
        monthRentIncome = monthRecordDao.getMonthIncome(parkId, num);
        map.put("august", new BigDecimal(parkIncome).add(new BigDecimal(reserveIncome)).add(new BigDecimal(monthRentIncome)).toString() );
        //  9
        num = 9;
        parkIncome = orderInfoDao.getMonthIncome(parkId, num);
        reserveIncome = parkReserveRecordDao.getMonthIncome(parkId, num);
        monthRentIncome = monthRecordDao.getMonthIncome(parkId, num);
        map.put("september", new BigDecimal(parkIncome).add(new BigDecimal(reserveIncome)).add(new BigDecimal(monthRentIncome)).toString() );
        //  10
        num = 10;
        parkIncome = orderInfoDao.getMonthIncome(parkId, num);
        reserveIncome = parkReserveRecordDao.getMonthIncome(parkId, num);
        monthRentIncome = monthRecordDao.getMonthIncome(parkId, num);
        map.put("october", new BigDecimal(parkIncome).add(new BigDecimal(reserveIncome)).add(new BigDecimal(monthRentIncome)).toString() );
        //  11
        num = 11;
        parkIncome = orderInfoDao.getMonthIncome(parkId, num);
        reserveIncome = parkReserveRecordDao.getMonthIncome(parkId, num);
        monthRentIncome = monthRecordDao.getMonthIncome(parkId, num);
        map.put("november", new BigDecimal(parkIncome).add(new BigDecimal(reserveIncome)).add(new BigDecimal(monthRentIncome)).toString() );
        //  12
        num = 12;
        parkIncome = orderInfoDao.getMonthIncome(parkId, num);
        reserveIncome = parkReserveRecordDao.getMonthIncome(parkId, num);
        monthRentIncome = monthRecordDao.getMonthIncome(parkId, num);
        map.put("december", new BigDecimal(parkIncome).add(new BigDecimal(reserveIncome)).add(new BigDecimal(monthRentIncome)).toString() );

        return map;
    }

    /**
     * 预约、泊车、月租总收益
     */
    @Override
    public Map income(Long parkId) {
        //  泊车收益
        String paidAmountByDate = orderInfoDao.getPaidAmountByDate(parkId, 5);
        //  预支付收益
        String reserveAmountByDate = parkReserveRecordDao.getReserveAmountByDate(parkId, 5);
        //  月租收益
        String monthRecordAmountByDate = monthRecordDao.getMonthRecordAmountByDate(parkId, 5);

        Map map = new HashMap();
        map.put("parkIncome", paidAmountByDate);
        map.put("reserveIncome", reserveAmountByDate);
        map.put("monthIncome", monthRecordAmountByDate);

        return map;
    }

}
